﻿using System.Collections;
using System.Collections.Generic; //　使用List

using UnityEngine;

//********** 修正开始 **********//
using UnityEngine.UI; // 使用UI依赖
//********** 修正结束 **********//

public class GameManager : MonoBehaviour {

    //********** 修正开始 **********//
    public float levelStartDelay = 2f; //level显示画面持续2秒
    //********** 修正结束 **********//

    public float turnDelay = .1f; //Enemy的动作耗时(0.1秒)

    //static变量：程序/场景之间共享
    //游戏中单独的变量
    //不由游戏对象所属，由程序控制所属
    public static GameManager instance = null;
    public BoardManager boardScript;

    public int playerFoodPoints = 100; //Player的体力
    //HideInInspector: 修饰public变量但是无法让Inspector操作
    //Player回合判定
    [HideInInspector] public bool playersTurn = true;

    //********** 修正开始 **********//
    private Text levelText; //level文本
    private GameObject levelImage; //level背景图片
    private int level = 1; //level初始化设置成1
    private bool doingSetup; // 初始化执行中的flag（用来禁止玩家/敌人的活动）
    //********** 修正结束 **********//

    private List<Enemy> enemies; //Enemy序列
    private bool enemiesMoving; //Enemy的回合中是true

    void Awake ( ) {

        //游戏开始时GameManager指定到instance里
        if (instance == null) {
            instance = this;
            //如果这个对象之外还有别的GameManager的时候销毁自己
        } else if (instance != this) {
            //销毁
            Destroy (gameObject);
        }
        //场景迁移的时候保留当前对象
        DontDestroyOnLoad (gameObject);

        //生成容纳Enemy的序列
        enemies = new List<Enemy> ( );

        boardScript = GetComponent<BoardManager> ( );
        InitGame ( );
    }

    //********** 修正开始 **********//
    //利用Unity的生命周期API、每当运行Scene时会被执行的方法
    private void OnLevelWasLoaded (int index) {
        level++; //level设置成1
        InitGame ( );
    }
    //********** 修正结束 **********//

    void InitGame ( ) {

        //********** 修正开始 **********//
        //true的时候、禁止控制Player
        doingSetup = true;
        //LevelImage对象・LevelText对象的获取
        levelImage = GameObject.Find ("LevelImage");
        levelText = GameObject.Find ("LevelText").GetComponent<Text> ( );
        levelText.text = "Day " + level; //更新至最新的level
        levelImage.SetActive (true); //控制LebelImage激活用于显示
        Invoke ("HideLevelImage", levelStartDelay); //2秒后调用HideLevelImage方法
        //********** 修正结束 **********//

        enemies.Clear ( ); //Enemy的List(序列)初始化

        boardScript.SetupScene (level);
    }

    //********** 修正开始 **********//
    private void HideLevelImage ( ) {
        levelImage.SetActive (false); // 关闭LevelImage
        doingSetup = false; // 使Player可以被控制
    }
    //********** 修正结束 **********//

    public void GameOver ( ) {

        //********** 修正开始 **********//
        // GameOver的信息
        levelText.text = "After " + level + " days, you starved.";
        levelImage.SetActive (true);
        //********** 修正结束 **********//

        //使GameManager无效
        enabled = false;
    }

    void Update ( ) {

        //********** 修正开始 **********//
        //doingSetup/Player的回合或者Enemy敌人的行动时不执行更新Update/=
        if (playersTurn || enemiesMoving || doingSetup) {
            //********** 修正结束 **********//
            return;
        }

        StartCoroutine (MoveEnemies ( ));
    }

    public void AddEnemyToList (Enemy script) {
        enemies.Add (script);
    }

    // 敌人移动
    IEnumerator MoveEnemies ( ) {
        enemiesMoving = true;
        yield return new WaitForSeconds (turnDelay);
        if (enemies.Count == 0) {
            yield return new WaitForSeconds (turnDelay);
        }
        //执行Enemy数量的Enemy脚本的MoveEnemy
        for (int i = 0; i < enemies.Count; i++) {
            enemies [i].MoveEnemy ( );
            yield return new WaitForSeconds (enemies [i].moveTime);
        }

        playersTurn = true;
        enemiesMoving = false;
    }

}
